using System.Collections.Generic;
using UnityEngine;
using QFramework;

namespace QFramework.Monk
{
	public partial class SimpleSword : AbilityBase
	{
		public override void InitAbility()
		{
			//mLevel = 0;//默认解锁
			//this.Show();
			//base.ShowAbility();
		}

		void Update()
		{
			if (!AppStart.CheckGameState(GameState.GamePlaying))
			{
				return;
			}

			mCurTime += Time.deltaTime;
			if (mCurTime > AppStart.ackDuration.Value) // 修正逻辑
			{
				var enemies = FindNearestEnemies(); // 更高效的敌人查找方法
				if (enemies.Count > 0)
				{
					foreach (var e in enemies)
					{
						if (e == null) continue; // 防止空指针异常
						//e.CostHp(AppStart.damage.Value);
						CreateSword(e);
					}
				}

				mCurTime = 0;
			}
		}

		private void CreateSword(EnemyBase e)
		{
			Sword.Instantiate().LocalScale(mIsSuper? 2:1)
				.Position(e.Position() + Vector3.one * 0.1f).Self((
					self =>
					{
						var selfCache = self;
						selfCache.OnTriggerEnter2DEvent(collider2D =>
						{
							if (collider2D.gameObject.layer == LayerMask.NameToLayer("Enemy"))
							{
								e.CostHp(AppStart.damage.Value);
								//selfCache.gameObject.DestroySelfGracefully();
							}

						}).UnRegisterWhenGameObjectDestroyed(selfCache.gameObject);

					})).Show();
		}
		
	}
}
